package cn.iocoder.an.module.business.controller.admin.equipmentchecktype;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepsettingtype.vo.EquipmentKeepSettingTypePageReqVO;
import cn.iocoder.an.module.business.controller.admin.equipmentkeepsettingtype.vo.EquipmentKeepSettingTypeRespVO;
import cn.iocoder.an.module.business.controller.admin.equipmentkeeptype.vo.EquipmentKeepTypeImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentchecktype.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentchecktype.EquipmentCheckTypeDO;
import cn.iocoder.an.module.business.service.equipmentchecktype.EquipmentCheckTypeService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 点检项设置")
@RestController
@RequestMapping("/business/equipment-check-type")
@Validated
public class EquipmentCheckTypeController {

    @Resource
    private EquipmentCheckTypeService equipmentCheckTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建点检项设置")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-type:create')")
    public CommonResult<Long> createEquipmentCheckType(@Valid @RequestBody EquipmentCheckTypeSaveReqVO createReqVO) {
        return success(equipmentCheckTypeService.createEquipmentCheckType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点检项设置")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-type:update')")
    public CommonResult<Boolean> updateEquipmentCheckType(@Valid @RequestBody EquipmentCheckTypeSaveReqVO updateReqVO) {
        equipmentCheckTypeService.updateEquipmentCheckType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点检项设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-check-type:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckType(@RequestParam("id") Long id) {
        equipmentCheckTypeService.deleteEquipmentCheckType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除点检项设置")
                @PreAuthorize("@ss.hasPermission('business:equipment-check-type:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckTypeList(@RequestParam("ids") List<Long> ids) {
        equipmentCheckTypeService.deleteEquipmentCheckTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得点检项设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-type:query')")
    public CommonResult<EquipmentCheckTypeRespVO> getEquipmentCheckType(@RequestParam("id") Long id) {
        EquipmentCheckTypeDO equipmentCheckType = equipmentCheckTypeService.getEquipmentCheckType(id);
        return success(BeanUtils.toBean(equipmentCheckType, EquipmentCheckTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得点检项设置分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-type:query')")
    public CommonResult<PageResult<EquipmentCheckTypeRespVO>> getEquipmentCheckTypePage(@Valid EquipmentCheckTypePageReqVO pageReqVO) {
        PageResult<EquipmentCheckTypeDO> pageResult = equipmentCheckTypeService.getEquipmentCheckTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentCheckTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点检项设置 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentCheckTypeExcel(@Valid EquipmentCheckTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentCheckTypeDO> list = equipmentCheckTypeService.getEquipmentCheckTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点检项设置.xls", "数据", EquipmentCheckTypeRespVO.class,
                        BeanUtils.toBean(list, EquipmentCheckTypeRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<EquipmentCheckTypeImportExcelVO> list = Arrays.asList(
                EquipmentCheckTypeImportExcelVO.builder().name("1安全事项")
                        .status(MaintenanceStatusEnum.run.getStatus()).description("1.压力表、安全阀检验检测应在有效期内。2.操作人员应持有效特种设备作业上岗证。").build()
        );
        // 输出
        ExcelUtils.write(response, "点检项导入模板.xlsx", "列表", EquipmentCheckTypeImportExcelVO.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<EquipmentCheckTypeImportExcelVO> list = ExcelUtils.read(file, EquipmentCheckTypeImportExcelVO.class);
        return success(equipmentCheckTypeService.importList(list, updateSupport));
    }


    @GetMapping("/mapList")
    @Operation(summary = "获得已绑定的和未绑定的数据")
    public CommonResult<Map<String, List<EquipmentCheckTypeRespVO>>> getEquipmentKeepSettingTypeMapList(@Valid EquipmentCheckTypePageReqVO pageReqVO) {
        return success(equipmentCheckTypeService.getEquipmentCheckTypeMapList(pageReqVO));
    }


    @GetMapping("/checkMapList")
    @Operation(summary = "获得已绑定的和未绑定的数据")
    public CommonResult<Map<String, List<EquipmentCheckTypeRespVO>>> checkMapList(@Valid EquipmentCheckTypePageReqVO pageReqVO) {
        return success(equipmentCheckTypeService.checkMapList(pageReqVO));
    }

}